package com.source.juccthread;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
 * @Author wengqy
 * @Date 2021/1/20
 */
@Slf4j
public class FixDemo {
    public static  int clientTotal = 10;
    public static  int threadTotal = 2;

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        Semaphore semaphore  = new Semaphore(threadTotal);
        CountDownLatch countDownLatch =  new CountDownLatch(clientTotal);
        for (int i = 0; i < clientTotal; i++) {
           int temp = i;
            executorService.execute(()->{
                try {
                    semaphore.acquire(1);
                    resolveww(temp);

                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    semaphore.release(1);
                    countDownLatch.countDown();
                }
            });

        }

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("跳出循环外。。。");
        executorService.shutdown();


    }

    private static void resolveww(int temp) {
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("服务受理中。。。。{}",temp);

        // 从输出结果可以看出，每隔两秒，有两个线程被执行
        /*D:\software\java\jdk\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=3313:C:\Program Files\JetBrains\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath D:\software\java\jdk\jre\lib\charsets.jar;D:\software\java\jdk\jre\lib\deploy.jar;D:\software\java\jdk\jre\lib\ext\access-bridge-64.jar;D:\software\java\jdk\jre\lib\ext\cldrdata.jar;D:\software\java\jdk\jre\lib\ext\dnsns.jar;D:\software\java\jdk\jre\lib\ext\jaccess.jar;D:\software\java\jdk\jre\lib\ext\jfxrt.jar;D:\software\java\jdk\jre\lib\ext\localedata.jar;D:\software\java\jdk\jre\lib\ext\nashorn.jar;D:\software\java\jdk\jre\lib\ext\sunec.jar;D:\software\java\jdk\jre\lib\ext\sunjce_provider.jar;D:\software\java\jdk\jre\lib\ext\sunmscapi.jar;D:\software\java\jdk\jre\lib\ext\sunpkcs11.jar;D:\software\java\jdk\jre\lib\ext\zipfs.jar;D:\software\java\jdk\jre\lib\javaws.jar;D:\software\java\jdk\jre\lib\jce.jar;D:\software\java\jdk\jre\lib\jfr.jar;D:\software\java\jdk\jre\lib\jfxswt.jar;D:\software\java\jdk\jre\lib\jsse.jar;D:\software\java\jdk\jre\lib\management-agent.jar;D:\software\java\jdk\jre\lib\plugin.jar;D:\software\java\jdk\jre\lib\resources.jar;D:\software\java\jdk\jre\lib\rt.jar;D:\software\java\jdk;D:\000ideapro\gitlabdailywork\gitlab_java\multithread\target\classes;D:\maven_responity6\org\springframework\boot\spring-boot-starter-web\2.4.1\spring-boot-starter-web-2.4.1.jar;D:\maven_responity6\org\springframework\boot\spring-boot-starter\2.4.1\spring-boot-starter-2.4.1.jar;D:\maven_responity6\org\springframework\boot\spring-boot\2.4.1\spring-boot-2.4.1.jar;D:\maven_responity6\org\springframework\boot\spring-boot-autoconfigure\2.4.1\spring-boot-autoconfigure-2.4.1.jar;D:\maven_responity6\org\springframework\boot\spring-boot-starter-logging\2.4.1\spring-boot-starter-logging-2.4.1.jar;D:\maven_responity6\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\maven_responity6\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\maven_responity6\org\apache\logging\log4j\log4j-to-slf4j\2.13.3\log4j-to-slf4j-2.13.3.jar;D:\maven_responity6\org\apache\logging\log4j\log4j-api\2.13.3\log4j-api-2.13.3.jar;D:\maven_responity6\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;D:\maven_responity6\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\maven_responity6\org\yaml\snakeyaml\1.27\snakeyaml-1.27.jar;D:\maven_responity6\org\springframework\boot\spring-boot-starter-json\2.4.1\spring-boot-starter-json-2.4.1.jar;D:\maven_responity6\com\fasterxml\jackson\core\jackson-databind\2.11.3\jackson-databind-2.11.3.jar;D:\maven_responity6\com\fasterxml\jackson\core\jackson-annotations\2.11.3\jackson-annotations-2.11.3.jar;D:\maven_responity6\com\fasterxml\jackson\core\jackson-core\2.11.3\jackson-core-2.11.3.jar;D:\maven_responity6\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.11.3\jackson-datatype-jdk8-2.11.3.jar;D:\maven_responity6\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.11.3\jackson-datatype-jsr310-2.11.3.jar;D:\maven_responity6\com\fasterxml\jackson\module\jackson-module-parameter-names\2.11.3\jackson-module-parameter-names-2.11.3.jar;D:\maven_responity6\org\springframework\boot\spring-boot-starter-tomcat\2.4.1\spring-boot-starter-tomcat-2.4.1.jar;D:\maven_responity6\org\apache\tomcat\embed\tomcat-embed-core\9.0.41\tomcat-embed-core-9.0.41.jar;D:\maven_responity6\org\glassfish\jakarta.el\3.0.3\jakarta.el-3.0.3.jar;D:\maven_responity6\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.41\tomcat-embed-websocket-9.0.41.jar;D:\maven_responity6\org\springframework\spring-web\5.3.2\spring-web-5.3.2.jar;D:\maven_responity6\org\springframework\spring-beans\5.3.2\spring-beans-5.3.2.jar;D:\maven_responity6\org\springframework\spring-webmvc\5.3.2\spring-webmvc-5.3.2.jar;D:\maven_responity6\org\springframework\spring-aop\5.3.2\spring-aop-5.3.2.jar;D:\maven_responity6\org\springframework\spring-context\5.3.2\spring-context-5.3.2.jar;D:\maven_responity6\org\springframework\spring-expression\5.3.2\spring-expression-5.3.2.jar;D:\maven_responity6\junit\junit\4.12\junit-4.12.jar;D:\maven_responity6\org\hamcrest\hamcrest-core\2.2\hamcrest-core-2.2.jar;D:\maven_responity6\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;D:\maven_responity6\org\hamcrest\hamcrest\2.2\hamcrest-2.2.jar;D:\maven_responity6\org\springframework\spring-core\5.3.2\spring-core-5.3.2.jar;D:\maven_responity6\org\springframework\spring-jcl\5.3.2\spring-jcl-5.3.2.jar;D:\maven_responity6\commons-io\commons-io\2.6\commons-io-2.6.jar;D:\maven_responity6\commons-lang\commons-lang\2.6\commons-lang-2.6.jar;D:\maven_responity6\org\dom4j\dom4j\2.0.0\dom4j-2.0.0.jar;D:\maven_responity6\jaxen\jaxen\1.2.0\jaxen-1.2.0.jar com.source.juccthread.FixDemo
        19:36:47.735 [pool-1-thread-2] INFO com.source.juccthread.FixDemo - 服务受理中。。。。1
        19:36:47.735 [pool-1-thread-1] INFO com.source.juccthread.FixDemo - 服务受理中。。。。0
        19:36:49.754 [pool-1-thread-3] INFO com.source.juccthread.FixDemo - 服务受理中。。。。2
        19:36:49.754 [pool-1-thread-4] INFO com.source.juccthread.FixDemo - 服务受理中。。。。3
        19:36:51.757 [pool-1-thread-6] INFO com.source.juccthread.FixDemo - 服务受理中。。。。5
        19:36:51.757 [pool-1-thread-5] INFO com.source.juccthread.FixDemo - 服务受理中。。。。4
        19:36:53.764 [pool-1-thread-8] INFO com.source.juccthread.FixDemo - 服务受理中。。。。7
        19:36:53.764 [pool-1-thread-7] INFO com.source.juccthread.FixDemo - 服务受理中。。。。6
        19:36:55.774 [pool-1-thread-9] INFO com.source.juccthread.FixDemo - 服务受理中。。。。8
        19:36:55.774 [pool-1-thread-10] INFO com.source.juccthread.FixDemo - 服务受理中。。。。9
        跳出循环外。。。*/



     }
}
